package operate;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import component.FormConstants;

import operate.vo.Form;
import util.Log;
import db.JdbcOperate;
import db.TableConstants;
@SuppressWarnings("unchecked")
public class FormOperate extends JdbcOperate{
	
	/**@Desc根据表单id形成html格式表单
	 * @param cid
	 * @return
	 */
	public static String createForm(String cid) {
		StringBuffer sb = new StringBuffer();
		try {
			List<Map> list = JdbcOperate
					.query("select * from form_attr t where t.cid=" + cid);

			for (int i = 0, length = list.size(); i < length; i++) {
				Map map = list.get(i);
				sb.append("<tr>");
				sb.append("<td class=\"TableContent\">");
				sb.append(map.get("ANAME"));
				sb.append("</td>");
				sb.append("<td>");
				sb.append(FormConstants.construt(map, 1));
				sb.append("</td>");
				sb.append("</tr>");
			}
		} catch (SQLException e) {
			Log.write(3, FormOperate.class, "创建表单视图出现问题", e);
		}
		return sb.toString();
	}
	
	/**@Desc根据表单id形成html格式表单
	 * @param cid
	 * @return
	 */
	public static String createUpdateForm(String cid,String id) {
		StringBuffer sb = new StringBuffer();
		try {
			List<Map> attrs = JdbcOperate
					.query("select * from form_attr t where t.update_view='1' and t.cid=" + cid);
			List<Map> datas=JdbcOperate.query("select * from t"+cid+" where id="+id);
            Map data=datas.get(0);
			for (int i = 0, length = attrs.size(); i < length; i++) {
				Map map = attrs.get(i);
				map.put("VALUE", data.get("F"+map.get("ID").toString()));
				sb.append("<tr>");
				sb.append("<td class=\"TableContent\">");
				sb.append(map.get("ANAME"));
				sb.append("</td>");
				sb.append("<td>");
				sb.append(FormConstants.construt(map, 2));
				sb.append("</td>");
				sb.append("</tr>");
			}
		} catch (SQLException e) {
			Log.write(3, FormOperate.class, "创建表单视图出现问题", e);
		}
		return sb.toString();
	}
	
	
	
	/**@Desc返回所有表单
	 * @return
	 */
	public static List<Form> getAllForm() {
		List<Form> forms = new ArrayList<Form>();
		try {
			List<Map> list = JdbcOperate.query("select * from "
					+ TableConstants.FORM + " where isdelete='0' order by cid asc");
			for (Map map : list) {
				Form form = new Form();
				form.setCid(map.get("CID").toString());
				form.setName(map.get("NAME").toString());
				forms.add(form);
			}
		} catch (SQLException e) {
			Log.write(3, Form.class, "查询所有表单出现错误", e);
		}
		return forms;
	}
	/**@Desc删除表单
	 * @param request
	 */
	public static void deleteForm(ServletRequest request) {
		String id = request.getParameter("id");
		try {
			JdbcOperate.delete(TableConstants.FORM, "id", id);
		} catch (SQLException e) {
			Log.warn(FormOperate.class, "删除表单出现错误", e);
		}
	}
	
	/**@Desc创建表单
	 * @param request
	 */
	public static void addForm(ServletRequest request) {
		String name = request.getParameter("name");
		StringBuffer sb = new StringBuffer();
		try {
			// 创建表
			String id=JdbcOperate.getSeq(TableConstants.FORM_SEQ);
			sb.append("create table t" + id + "(");
			sb.append(" id   number     not null,");
			sb.append(" create_time  date null,");
			sb.append(" update_time  date null);");
			// 插入表单数据
			sb.append("insert into " + TableConstants.FORM
					+ "(cid,name,isdelete)values(");
			sb.append(id + ",'" + name + "','0');");
			//建立索引
			sb.append("create sequence t"+id+"_seq");
			JdbcOperate.batchInsertOrUpdate(sb.toString());
		} catch (SQLException e) {
			Log.warn(FormOperate.class, "表单创建出现异常", e);
		}
	}
}
